/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-08-26
 * Time: 23:57
 * To BE A Better Person-2020-3-2
 */
class MyHashMap{
    public final double DEFAULT_LOAD_FACTOR = 0.75;
    public final int Default_Length=8;
    public Node[] array;
    public int useSize;
    static class Node{
        public int key;
        public int val;
        public Node next;
        public Node(int key,int val){
            this.key=key;
            this.val=val;
        }
    }

    public MyHashMap(){
        array=new Node[Default_Length];
    }

    public void put(int key,int val){
        Node node=new Node(key,val);
        int index=key%array.length;
        Node cur=array[index];
        //先看有没有重复的元素
        while(cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        //走到这里就代表没有重复元素
        //开始头插
        array[index]=insert(node,array[index]);
        useSize++;
        //检查负载因子是否过大
        if(getDEFAULT_LOAD_FACTOR()>DEFAULT_LOAD_FACTOR){
            //*********扩容*******
            resize();
        }
    }

    private Node insert(Node node,Node head){
        node.next=head;
        head=node;
        return head;
    }

    private double getDEFAULT_LOAD_FACTOR(){
        return useSize*1.0/ array.length;
    }

    private void resize(){
        Node[] temp=new Node[2*array.length];
        //遍历原数组
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while(cur!=null){
                int index= cur.key%(2*array.length);
                Node curNext=cur.next;
                temp[index]=insert(cur,temp[index]);
                cur=curNext;
            }
        }
        array=temp;
    }

    public int get(int key){
        return 0;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        MyHashMap myHashMap=new MyHashMap();
        myHashMap.put(2,99);
        myHashMap.put(13,99);
        myHashMap.put(1,20);
        myHashMap.put(6,19);
        myHashMap.put(20,9);
        myHashMap.put(5,10);
        System.out.println("sasssaasa");
    }
}
